<?php
    include('header.php');
    include('submenu.php');
    
    $m_id = $_POST['h_m_id'];
    $text_en = $_POST['text_en'];
    $text_th = $_POST['text_th'];
    
    
    $ms_path = '';
    if($m_id!=3){
        if($_FILES['news_img']['name']!='')
        {
            $handle = new Upload($_FILES['news_img']);
            if ($handle->uploaded)
            {
                $handle->image_resize = true;
                $handle->image_ratio_fill = true;
                $handle->image_background_color = '#ffffff';
                $handle->image_x = 700;
                $handle->image_y = 450;
                
                $handle->Process(APPPATH.'/images/media/'.$m_id);
                if ($handle->processed)
                {
                    $ms_path = $handle->file_dst_name;
                }
                else
                {
                    $msg = 'ไม่สามารถอัพโหลดรูปได้';
                }
    
            }
            else
            {
                $msg = 'ไม่สามารถอัพโหลดรูปได้';
            }
        }
    }else{
        $ms_path = $_POST['news_img'];
    }
    
    $sql = "INSERT INTO media_slide(m_id,ms_text_en,ms_text_th,ms_path) VALUES('$m_id','$text_en','$text_th','$ms_path')";
    $result = $db->Execute($sql);
    if($result)
    {
        $msg = 'บันทึกข้อมูลเรียบร้อยแล้ว';
    }
    else
    {
        if($m_id!=3){
            if(img_exists(APPPATH.'/images/media/'.$m_id,$handle->file_dst_name)){
                @unlink(APPPATH.'/images/media/'.$m_id.'/'.$handle->file_dst_name);
            }
        }
        $msg = 'ไม่สามารถจัดเก็บข้อมูลได้';
    }
    
?>
<div class="grid_18" id="contents">
    <div id="query_result">
    <?php
        echo $msg , ' <img src="images/ajax-loader.gif" />';
        redirect('?m=media-communications&m_id='.$m_id,3);
    ?>
    </div>
</div><!-- end .grid_18 -->
<div class="clear">&nbsp;</div>
<?php
    include('footer.php');
?>